package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartInfoFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


@Controller
public class CartInfoController {
    @Autowired
    private CartInfoFeignClient cartInfoFeignClient;

    //加入购物车 加
    @GetMapping("/addCart.html")
    //默认形参无需放到远程调用接口中 如RedirectAttributes的对象
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes) {
        System.out.println("进入加入成功页面");
        cartInfoFeignClient.addToCart(skuId, skuNum);
        redirectAttributes.addAttribute("sId", skuId);
        redirectAttributes.addAttribute("sn", skuNum);
        redirectAttributes.addAttribute("name", "我是好人");
        //为防止刷新时添加到购物车 进行重定向
        return "redirect:http://cart.gmall.com/toCart.html";
    }

    //加入购物车 查
    @GetMapping("/toCart.html")
    public String toCart(Long sId, Integer sn, String name, Model model) {
        System.out.println("回显加入页面");
        System.out.println("name = " + name);
        //表面上是向下强转,实质上是向上强转
        ExtendedModelMap emm = (ExtendedModelMap) model;
        CartInfo cartInfo = cartInfoFeignClient.toCart(sId);
        //页面回显本次加入的数量而不是总数量
        cartInfo.setSkuNum(sn);
        model.addAttribute("cartInfo", cartInfo);
        return "cart/addCart";
    }

    //去购物车结算
    @GetMapping("/cart.html")
    public String cartList() {
        return "cart/index";
    }
}
